﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using CustomControls.InheritedFrameworkControls;
using CustomControls.MiscControls;
using Infrasturcture.Global.Controls.Dialog;
using Infrasturcture.TouchLibrary;
using MallInteractiveMap.DTO;

namespace CustomControls.PhotoViewer
{
    /// <summary>
    /// Interaction logic for PhotoStack.xaml
    /// </summary>
    public partial class PhotoStack : ADialogContent, IMTouchControl
    {
        private const int _maximumPhotoInStack = 5;
        private List<DTOShopPhotos> _photoCollection;
        public event EventHandler<AddRemovePhotoEvntArgs> AddRemovedPhotos;

        private int _rear;
        private int Rear
        {
            set
            {
                _rear = value;
                LeftButton.IsEnabled = _rear != 0;
            }
            get { return _rear; }
        }

        private int _front;
        private int Front
        {
            set
            {
                _front = value;
                RightButton.IsEnabled = _front != _photoCollection.Count;
            }
            get { return _front; }
        }
        public PhotoStack()
        {
            InitializeComponent();
            
        }

        private double ThumbnailWidth
        {
            get
            {
                return (this.Width - 72)/5.0;
            }
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            //PopulatePhotos();
        }

        

        public void PopulatePhotos( List<DTOShopPhotos> photoCollection)
        {
            double thumbWidth = ThumbnailWidth;
            _photoCollection = photoCollection;
            bool setFirstItemChecked = true;
            int count = 0;
            foreach (var photo in _photoCollection)
            {
                AddPhotoToStack(photo, thumbWidth, setFirstItemChecked);
                setFirstItemChecked = false;
                count++;
                if(count == _maximumPhotoInStack)
                {
                    break;
                }
            }
            Rear = 0;
            Front = count;
        }
        public UIElementCollection PhotoCollection
        {
            get
            {
                return photos.Children;
            }
        }

        private ThumbnailPhoto AddPhotoToStack(DTOShopPhotos photo, double thumbWidth, bool setFirstItemChecked, bool isInsert = false)
        {
            ThumbnailPhoto tp = new ThumbnailPhoto();
            tp.ThumbnailClick += new EventHandler<ThumbnailEventArgs>(tp_ThumbnailClick);
            tp.ImageSource = BitmapFrame.Create(new MemoryStream(photo.ShopImage));
           // tp.ImageHeight = this.Height - 30;
            tp.ImageWidth = thumbWidth;
            if (isInsert)
            {
                photos.Children.Insert(0, tp);
            }
            else
            {
                photos.Children.Add(tp);
            }
            tp.IsSelected = setFirstItemChecked;
            return tp;
        }



        void tp_ThumbnailClick(object sender, ThumbnailEventArgs e)
        {
            mainPhoto.Source = e.ImageSource;
            mainPhoto.Width = this.Width/2.0;
        }
        public TouchImage MainPhotoView
        {
            get
            {
                return mainPhoto;
            }
        }

        private void RightButton_Click(object sender, RoutedEventArgs e)
        {
            //sclPhoto.ScrollToHorizontalOffset(sclPhoto.HorizontalOffset + ThumbnailWidth);
            var thumbnailPhoto = ((ThumbnailPhoto)photos.Children[0]);
            bool isSelected = thumbnailPhoto.IsSelected;
            photos.Children.RemoveAt(0);
            ThumbnailPhoto addedItem = AddPhotoToStack(_photoCollection[Front], ThumbnailWidth, isSelected);
            if(AddRemovedPhotos != null)
            {
                AddRemovedPhotos(sender,
                                 new AddRemovePhotoEvntArgs() { AddedItem = addedItem.PhotoButton, RemovedItem = thumbnailPhoto.PhotoButton });
            }
            Front++;
            Rear++;
           
        }

        private void LeftButton_Click(object sender, RoutedEventArgs e)
        {
            //sclPhoto.ScrollToHorizontalOffset(sclPhoto.HorizontalOffset - ThumbnailWidth);
            var thumbnailPhoto = ((ThumbnailPhoto)photos.Children[_maximumPhotoInStack - 1]);
            bool isSelected = thumbnailPhoto.IsSelected;
            photos.Children.RemoveAt(_maximumPhotoInStack-1);
            ThumbnailPhoto addedItem = AddPhotoToStack(_photoCollection[--Rear], ThumbnailWidth, isSelected, true);
            if (AddRemovedPhotos != null)
            {
                AddRemovedPhotos(sender,
                                 new AddRemovePhotoEvntArgs() { AddedItem = addedItem.PhotoButton, RemovedItem = thumbnailPhoto.PhotoButton });
            }
            Front--; 
        }

        public IMTContainer Container
        {
            get; set;
        }

       
        public TouchRepeatButton PhotoLeftButton
        {
            get
            {
                return LeftButton;
            }
        }
        public TouchRepeatButton PhotoRightButton
        {
            get
            {
                return RightButton;
            }
        }
    }
    public class AddRemovePhotoEvntArgs:EventArgs
    {
        public TouchRadioButton  AddedItem { get; set; }
        public TouchRadioButton RemovedItem { get; set; }
    }
}
